package com.hemaapp.yjnh.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.BusStep;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteBusLineItem;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.BusRouteQuery;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.RouteListAdapter;
import com.hemaapp.yjnh.bean.Route;

/**
 * 路线搜索页面
 */
public class RouteSearchActivity extends BaseActivity implements
		OnRouteSearchListener {
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private TextView temp_t1;
	private TextView temp_t2;
	private ImageButton turnbtn;
	private RadioGroup radioGroup;
	private ListView route_list;
//	private ProgressBar progressbar;

	private String toNameExra;
	private LatLonPoint toPointExra;
	private LatLonPoint fromPointExra;

	private RouteSearch routeSearch;

	private ArrayList<Route> busRoutes = new ArrayList<Route>();
	private ArrayList<Route> carRoutes = new ArrayList<Route>();
	private ArrayList<Route> walkRoutes = new ArrayList<Route>();

	private RouteListAdapter busAdapter;
	private RouteListAdapter carAdapter;
	private RouteListAdapter walkAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_routesearch);
		super.onCreate(savedInstanceState);
		routeSearch = new RouteSearch(this);
		routeSearch.setRouteSearchListener(this);
		busAdapter = new RouteListAdapter(this, busRoutes);
		busAdapter.setEmptyString("抱歉\n没有查到公交线路");
		carAdapter = new RouteListAdapter(this, carRoutes);
		carAdapter.setEmptyString("抱歉\n没有查到驾车线路");
		walkAdapter = new RouteListAdapter(this, walkRoutes);
		walkAdapter.setEmptyString("抱歉\n没有查到步行线路");
		startSearch();
	}

	private void startSearch() {
		route_list.setVisibility(View.INVISIBLE);
		showProgressDialog("请稍候");
		turnbtn.setOnClickListener(null);
		busRoutes.clear();
		carRoutes.clear();
		walkRoutes.clear();
		searchRouteResult(1);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ADVICE_ADD:
			showProgressDialog("正在提交您的宝贵意见");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ADVICE_ADD:
			// cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ADVICE_ADD:

			break;
		default:
			break;
		}

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ADVICE_ADD:
			// showTextDialog(baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ADVICE_ADD:
			// showTextDialog("意见提交失败");
			break;
		}
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		temp_t1 = (TextView) findViewById(R.id.temp_t1);
		temp_t2 = (TextView) findViewById(R.id.temp_t2);
		turnbtn = (ImageButton) findViewById(R.id.turnbtn);
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		route_list = (ListView) findViewById(R.id.route_list);
	}

	@Override
	protected void getExras() {
		toPointExra = mIntent.getParcelableExtra("toPoint");
		fromPointExra = mIntent.getParcelableExtra("fromPoint");
		toNameExra = mIntent.getStringExtra("toName");
	}

	@Override
	protected void setListener() {
		titleText.setText("查看线路");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);

		temp_t1.setText("我的位置");
		temp_t2.setText(toNameExra);

		radioGroup.setOnCheckedChangeListener(new CheckedChangeListener());
	}

	private class CheckedChangeListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.bus_btn:
				route_list.setAdapter(busAdapter);
				break;
			case R.id.car_btn:
				route_list.setAdapter(carAdapter);
				break;
			case R.id.walk_btn:
				route_list.setAdapter(walkAdapter);
				break;
			}
		}
	}

	/**
	 * 开始搜索路径规划方案
	 * 
	 * @param routeType
	 *            1公交,2驾车,3步行
	 */
	public void searchRouteResult(int routeType) {
		final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
				fromPointExra, toPointExra);
		if (routeType == 1) {// 公交路径规划
			BusRouteQuery query = new BusRouteQuery(fromAndTo,
					RouteSearch.BusDefault, getCityId(), 0);// 第一个参数表示路径规划的起点和终点，第二个参数表示公交查询模式，第三个参数表示公交查询城市区号，第四个参数表示是否计算夜班车，0表示不计算
			routeSearch.calculateBusRouteAsyn(query);// 异步路径规划公交模式查询
		} else if (routeType == 2) {// 驾车路径规划
			DriveRouteQuery query = new DriveRouteQuery(fromAndTo,
					RouteSearch.DrivingDefault, null, null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
			routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
		} else if (routeType == 3) {// 步行路径规划
			WalkRouteQuery query = new WalkRouteQuery(fromAndTo,
					RouteSearch.WalkDefault);
			routeSearch.calculateWalkRouteAsyn(query);// 异步路径规划步行模式查询
		}
	}

	// 公交路线搜索回调
	@Override
	public void onBusRouteSearched(BusRouteResult result, int arg1) {
		if (result != null && result.getPaths() != null
				&& result.getPaths().size() > 0) {
			List<BusPath> paths = result.getPaths();
			for (BusPath busPath : paths) {
				long duration = busPath.getDuration();
				String time = "预计" + BaseUtil.transDuration(duration);
				float dist = busPath.getDistance();
				String distance = "总计" + BaseUtil.transDistance(dist);
				List<BusStep> steps = busPath.getSteps();
				String name = "";
				for (BusStep busStep : steps) {
					RouteBusLineItem item = busStep.getBusLine();
					if (item != null) {
						String n = item.getBusLineName();
						String[] l = n.split("路");
						name += (l[0] + "路转");
					}
				}
				name = name.substring(0, name.length() - 1);
				busRoutes.add(new Route(name, time, distance, busPath, null,
						null, fromPointExra, toPointExra));
			}
		} else {
			log_i("公交路线查询失败");
		}
		searchRouteResult(2);
	}

	// 驾车路线搜索回调
	@Override
	public void onDriveRouteSearched(DriveRouteResult result, int arg1) {
		if (result != null && result.getPaths() != null
				&& result.getPaths().size() > 0) {
			List<DrivePath> paths = result.getPaths();
			for (DrivePath drivePath : paths) {
				long duration = drivePath.getDuration();
				String time = "预计" + BaseUtil.transDuration(duration);
				float dist = drivePath.getDistance();
				String distance = "总计" + BaseUtil.transDistance(dist);
				String name = distance + "," + time;
				carRoutes.add(new Route(name, time, distance, null, drivePath,
						null, fromPointExra, toPointExra));
			}
		} else {
			log_i("驾车路线查询失败");
		}
		searchRouteResult(3);
	}

	// 步行路线搜索回调
	@Override
	public void onWalkRouteSearched(WalkRouteResult result, int arg1) {
		if (result != null && result.getPaths() != null
				&& result.getPaths().size() > 0) {
			List<WalkPath> paths = result.getPaths();
			for (WalkPath walkPath : paths) {
				long duration = walkPath.getDuration();
				String time = "预计" + BaseUtil.transDuration(duration);
				float dist = walkPath.getDistance();
				String distance = "总计" + BaseUtil.transDistance(dist);
				String name = distance + "," + time;
				walkRoutes.add(new Route(name, time, distance, null, null,
						walkPath, fromPointExra, toPointExra));
			}
		} else {
			log_i("步行路线查询失败");
		}
		serachFinish();
	}

	private void serachFinish() {
		route_list.setVisibility(View.VISIBLE);
		cancelProgressDialog();
		int checkedId = radioGroup.getCheckedRadioButtonId();
		switch (checkedId) {
		case R.id.bus_btn:
			route_list.setAdapter(busAdapter);
			break;
		case R.id.car_btn:
			route_list.setAdapter(carAdapter);
			break;
		case R.id.walk_btn:
			route_list.setAdapter(walkAdapter);
			break;
		}
		turnbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LatLonPoint temPoint = toPointExra;
				toPointExra = fromPointExra;
				fromPointExra = temPoint;
				String m = temp_t1.getText().toString();
				String t = temp_t2.getText().toString();
				temp_t2.setText(m);
				temp_t1.setText(t);
				startSearch();
			}
		});
	}

}
